﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class CinemaData
    {
        //对于电影院的数据的操作类
        public static IQueryable<Cinemas> Show(string area)
        {
            ProjectDBEntities efContext = new ProjectDBEntities();
            var list = from c in efContext.Cinemas where c.area == area select c;
            return list;
        }

        public Cinemas Hot()
        {
            //  ////创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //查询电影评分表取出电影院的id和对应电影院所有评分的平均值存入list中
            var list = from c in pde.ScoresOfCinemas
                       group c by c.c_id into g
                       orderby g.Key
                       select new
                       {
                           CinemaId = g.FirstOrDefault().c_id,
                           AvgScore = (from s in pde.ScoresOfCinemas
                                       where s.c_id == g.Key
                                       select s.score).Average()

                       };
            //声明int变量id存储电影院id
            //声明int变量score存储电影院平均分
            int id = 0;
            int score = 0;
            //遍历list，取出最打的平均分和其对应的电影院id，分别放入id和score变量中
            foreach (var l in list)
            {
                if (l.AvgScore >= score)
                {
                    score = Convert.ToInt32(l.AvgScore);
                    id = Convert.ToInt32(l.CinemaId);
                }
            }
            //查询数据库，在电影表查到对应评分最高的电影院的对象
            var hot = from c in pde.Cinemas
                      where c.cinema_id == id
                      select c;
            if (id == 0)
            {
                hot = from c in pde.Cinemas
                      select c;
            }
            //返回该对象
            foreach (Cinemas c in hot)
            {
                return c;
            }

            return null;
        }
        //显示对应电影院电影函数
        public IQueryable<MovieLists> Smovie(string name)
        {
            //  ////创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //在Movies中查询查询MovieLists表中对应的当前电影院id的电影集合，返回该集合列表
            var m = from v in pde.MovieLists
                    where v.cinema == name
                    select v;
            return m;
        }
        public IQueryable<Cinemas> MessageData()
        {
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //用linq语句查询出所有的电影yuan
            var u = from c in pde.Cinemas

                    select c;

            return u;

        }

        //根据影院名字提取影院信息
        public Cinemas CinemaInfoData(string cinemaName)
        {
            //创建上下文对象，用来操作数据库
            ProjectDBEntities pde = new ProjectDBEntities();
            //用linq语句查询出对应输入的用户名的对象
            var u = from c in pde.Cinemas
                    where c.name == cinemaName
                    select c;
            foreach (Cinemas n in u)
            {
                return n;
            }
            return null;
        }
    }
}
